﻿#ifndef CUSTOM_UTIL_PASS_INCLUDED
#define CUSTOM_UTIL_PASS_INCLUDED
#include "../ShaderLibrary/UnityInput.hlsl"

float SmoothCurve(float x) {
	return x * x * (3.0 - 2.0 * x);
}

float TriangleWave(float x) {
	return abs(frac(x + 0.5) * 2.0 - 1.0);
}

float SmoothTriangleWave(float x) {
	return SmoothCurve(TriangleWave(x));
}

float4 WindGrassWave(float4 posWorld, float vColorA, float _WindEdgeFlutterFreqScale, float4 _Wind, 
	float _ColliderForce, float4 _PlayerPos, float _ColliderRadius, float _PlayerForce, float _ForceY)
{
	float windTime = _Time.y * _WindEdgeFlutterFreqScale;

	// Wave 1 :XZ wave
	// Wave 2 detail:add X wave!
	half windPara = vColorA * _Wind.w;

	half colliderForce = vColorA * SmoothTriangleWave(3 * windTime) * _ColliderForce;

	half oriX = SmoothTriangleWave(windTime + posWorld.x) * windPara;
	half oriZ = SmoothTriangleWave(windTime + posWorld.z) * windPara;

	posWorld.x += oriX;
	posWorld.z += oriZ;

	//#ifdef _PLANTCOLLIDER
	half3 posP = _PlayerPos.xyz;
	half moveThread = _ColliderRadius - distance(posWorld.xyz, posP);
	moveThread = clamp(moveThread, 0, 1);

	half2 forceDir = normalize(posWorld.xz - posP);

	posWorld.xz += forceDir * windPara * moveThread * _PlayerForce;
	posWorld.y += lerp(0, _ForceY, moveThread);

	return posWorld;
}

//void vert(inout appdata_full v, out Input o) {
//	UNITY_INITIALIZE_OUTPUT(Input, o);
//	float4 posWorld = mul(unity_ObjectToWorld, v.vertex);
//
//
//	float windTime = _Time.y * _WindEdgeFlutterFreqScale;
//
//	// Wave 1 :XZ wave
//	// Wave 2 detail:add X wave!
//	half windPara = v.color.a * _Wind.w;
//
//	half colliderForce = v.color.a * SmoothTriangleWave(3 * windTime) * _ColliderForce;
//
//	half oriX = SmoothTriangleWave(windTime + posWorld.x) * windPara;
//	half oriZ = SmoothTriangleWave(windTime + posWorld.z) * windPara;
//
//	posWorld.x += oriX;
//	posWorld.z += oriZ;
//
//	//#ifdef _PLANTCOLLIDER
//	half3 posP = _PlayerPos.xyz;
//	half moveThread = _ColliderRadius - distance(posWorld.xyz, posP);
//	moveThread = clamp(moveThread, 0, 1);
//
//	half2 forceDir = normalize(posWorld.xz - posP);
//
//	posWorld.xz += forceDir * windPara * moveThread * _PlayerForce;
//	posWorld.y += lerp(0, _ForceY, moveThread);
//	//#endif
//	v.vertex = mul(unity_WorldToObject, posWorld);
//
//	half NdotL = dot(v.normal, _WorldSpaceLightPos0.xyz);
//	// Figure out if we should use the inverse normal or the regular normal based on light direction.
//}
//	o.UVdiff.z = (NdotL > 0) ? NdotL : dot(-v.normal, _WorldSpaceLightPos0.xyz);
//	o.UVdiff.xy = TRANSFORM_TEX(v.texcoord, _MainTex);


//
//void vert_veg(inout appdata_full v, out Input o)
//{
//	UNITY_INITIALIZE_OUTPUT(Input, o);
//#ifdef USE_VERTEXANIM
//	// Wave 1
//	float3 winDirection = float3(1, 0.5, 0.5);
//	v.vertex.xyz += normalize(winDirection + v.normal) * v.color.g * sin(((v.color.b * 3.141592654) + _Time)) * 0.08;// * _GlobalWindAmount;
//	v.vertex.x += sin((v.vertex.y + _Time * _VertAnim_Speed) * _VertAnim_Frequency) * _VertAnim_Offset * v.color.a;// * _GlobalWindAmount;
//
//#endif
//
//	float4 pos = UnityObjectToClipPos(v.vertex);
//	UNITY_TRANSFER_FOG(o, pos);
//
//	o.occ = lerp(1, (1 - v.color.r), _OccAmount);
//	o.emiCol = v.color.g * _EmissionColor.rgb * _EmissionVal;
//}
//
//float3 VegetationTreeAnimation(float3 normal, float4 color, float)
//{
//	float3 winDirection = float3(1, 0.5, 0.5);
//	v.vertex.xyz += normalize(winDirection + v.normal) * v.color.g * sin(((v.color.b * 3.141592654) + _Time)) * 0.08;// * _GlobalWindAmount;
//	v.vertex.x += sin((v.vertex.y + _Time * _VertAnim_Speed) * _VertAnim_Frequency) * _VertAnim_Offset * v.color.a;// * _GlobalWindAmount;
//}

#endif